package pers.itqiqi.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pers.itqiqi.dao.UserDao;
import pers.itqiqi.service.UserService;

/**
 * @author kevin_琪琪
 * @version 1.0
 * @description
 * @date 2022-07-08 09:47
 */

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    /**
     * 转账
     * @param from 转账发起用户id
     * @param to 转账收到用户id
     * @param money 转账金额
     */
    @Override
    @Transactional
    public void transferMoney(Long from,Long to, Long money) {
        Long fromMoney = userDao.queryMoney(from);
        Long toMoney = userDao.queryMoney(to);
        if (fromMoney < money) {
            System.out.println("您的余额不足不可以转账！");
        } else {
            userDao.updateMoney(fromMoney-money,from);
            userDao.updateMoney(toMoney+money, from);
            System.out.println("转账成功！");
        }
    }
}
